<?php

namespace App\Models;

/**
 * Class Withdraw
 * @property User user;
 */
class Withdraw extends ApplicationModel
{
    protected $fillable = [
        'ch_id',
        'object',
        'type',
        'time_transferred',
        'livemode',
        'status',
        'app',
        'channel',
        'order_no',
        'amount',
        'amount_settle',
        'currency',
        'recipient',
        'description',
        'transaction_no',
        'failure_msg',
        'user_name',
        'force_check',
        'user_id',
    ];


    public function status_desc()
    {
        if (isset($this->failure_msg) && !empty($this->failure_msg)) {
            return '提现失败';
        }
        if ($this->status == 'pending') {
            return "处理中";
        }
        if ($this->status == 'paid') {
            return "提现成功";
        }
        if ($this->status == 'failed') {
            return "提现失败";
        }
        if ($this->status == 'scheduled') {
            return "待发送";
        }
        if ($this->status == 'audit_fail') {
            return "审核未通过";
        }

    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }


    public static function saveWithdraw($tr)
    {
        $withdraw = Withdraw::where('order_no', $tr['order_no'])->first();
        if (!$withdraw) {
            $withdraw = new Withdraw;
        }
        $withdraw->ch_id = isset($tr['id']) ? $tr['id'] : '';
        $withdraw->object = isset($tr['object']) ? $tr['object'] : '';
        $withdraw->type = isset($tr['type']) ? $tr['type'] : '';
        $withdraw->time_transferred = isset($tr['time_transferred']) ? $tr['time_transferred'] : '';
        $withdraw->livemode = isset($tr['livemode']) ? $tr['livemode'] : '';
        $withdraw->status = isset($tr['status']) ? $tr['status'] : '';
        $withdraw->app = isset($tr['app']) ? $tr['app'] : '';
        $withdraw->channel = isset($tr['channel']) ? $tr['channel'] : '';
        $withdraw->order_no = isset($tr['order_no']) ? $tr['order_no'] : '';
        $withdraw->amount = isset($tr['amount']) ? $tr['amount'] : '';
        $withdraw->amount_settle = isset($tr['amount_settle']) ? $tr['amount_settle'] : '';
        $withdraw->currency = isset($tr['currency']) ? $tr['currency'] : '';
        $withdraw->recipient = isset($tr['recipient']) ? $tr['recipient'] : '';
        if (isset($tr['description'])) {
            $withdraw->description = $tr['description'];
        }
        if (isset($tr['nickname'])) {
            $withdraw->user_name = $tr['nickname'];
        }
        $withdraw->transaction_no = isset($tr['transaction_no']) ? $tr['transaction_no'] : '';
        $withdraw->failure_msg = isset($tr['failure_msg']) ? $tr['failure_msg'] : '';
        $withdraw->force_check = isset($tr['force_check']) ? $tr['force_check'] : '';
        $withdraw->user_id = isset($tr['user_id']) ? $tr['user_id'] : '';
        $withdraw->save();

        return $withdraw;
    }

    public function send_msg($content, $url = null)
    {
        //发送推送
        $message = new Message();
        $message->user_id = $this->user_id;
        $message->content = $content;
        $message->icon = Message::ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->save();

        $message->pushMsg($this->user_id, $content, "{'url':'" . $url . "'}");
    }

}
